package com.hengtiansoft.lfy.jpush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.hengtiansoft.lfy.activity.MainActivity;
import com.hengtiansoft.lfy.activity.NewsDetailActivity;
import com.hengtiansoft.lfy.constants.Constants;
import com.hengtiansoft.lfy.entity.NewsEntity;
import com.umeng.socialize.media.Constant;

import cn.jpush.android.api.JPushInterface;

/**
 * 自定义接收器
 * 
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
//		Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
		
        if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
			String message = bundle.getString("cn.jpush.android.EXTRA");

			NewsEntity.DataEntity data;
			if (!TextUtils.isEmpty(message)) {
				data = new Gson().fromJson(message, NewsEntity.DataEntity.class);
			} else {
				return;
			}

//			Intent i = new Intent(context, NewsDetailActivity.class);
//			i.putExtra(NewsDetailActivity.EXTRA_NEWS, data);
//			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//			context.startActivity(i);


			//0代表新闻,1代表试卷
			if(data == null || data.getJm_type()== null)return;
			int code = Integer.parseInt(data.getJm_type());
			Log.d("tag","code = "+code);
			if(code == 0){
				Intent i = new Intent(context, NewsDetailActivity.class);
				i.putExtra(NewsDetailActivity.EXTRA_NEWS, data);
				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(i);
			}else{
				//{"content":"练习内容有更新","jm_type":"1"}
				Intent i = new Intent(context, MainActivity.class);
				i.putExtra(Constants.EVALUATOR_MESSAGE, Constants.EVALUATOR_MESSAGE);
				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(i);
			}


        }
	}
}
